package thread;
//      juc的缩写    java并发包
import javax.xml.transform.Source;
import java.sql.SQLOutput;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池示例
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        //创建一个具有两条线程的线程池
        ExecutorService threadPool= Executors.newFixedThreadPool(3);

        for (int i = 0; i < 5; i++){
            //重写线程的run方法
            Runnable r=new Runnable() {
                @Override
                public void run() {
                    try {
                        //
                        Thread t=Thread.currentThread();
                        System.out.println(t.getName()+"正在执行...");
                        Thread.sleep(5000);
                        System.out.println(t.getName()+"执行完毕...");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
            //将任务交给线程池
            threadPool.execute(r);
            System.out.println("将一个任务交给了线程池");
        }
        //threadPool.shutdownNow();//立即关闭线程池 中断任务 直接报错
        threadPool.shutdown();//关闭线程池 不再接任务
        System.out.println("线程池关闭...");

    }
}
